﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        public void YGRHRPDCXS(DataSet db)
        {
            int x = 4, y = 1;
            string HRITPNR = "";
            DataTable dtOutput = new DataTable();
            try
            {
                #region======Title===========
                int idx = 0;
                int start = 4;
                exAgent.SetCellValue(start, idx++, "工号"); dtOutput.Columns.Add("HRITPNR", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "姓名"); dtOutput.Columns.Add("HRVNAME", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "岗位"); dtOutput.Columns.Add("HRZARTDES", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "在职状态"); dtOutput.Columns.Add("HRARTERLDES", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "工作经验"); dtOutput.Columns.Add("KDKUNDNA", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "产量品种"); dtOutput.Columns.Add("ARTTYPE", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "单价"); dtOutput.Columns.Add("UNITPRICE", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "产量合计"); dtOutput.Columns.Add("QTY", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "原始产量工资"); dtOutput.Columns.Add("BSALARY", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "出勤天数"); dtOutput.Columns.Add("WORKDAYS", typeof(System.Decimal));
                exAgent.SetCellValue(start, idx++, "数据调整"); dtOutput.Columns.Add("QTYADJUST", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "实发产量工资"); dtOutput.Columns.Add("SALARY", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "实发出勤工资"); dtOutput.Columns.Add("WORKSALARY", typeof(System.Decimal));
                exAgent.SetCellValue(start, idx++, "原日均工资"); dtOutput.Columns.Add("BDAILY", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "实发日均工资"); dtOutput.Columns.Add("RDAILY", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "01"); dtOutput.Columns.Add("BS01", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "02"); dtOutput.Columns.Add("BS02", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "03"); dtOutput.Columns.Add("BS03", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "04"); dtOutput.Columns.Add("BS04", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "05"); dtOutput.Columns.Add("BS05", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "06"); dtOutput.Columns.Add("BS06", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "07"); dtOutput.Columns.Add("BS07", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "08"); dtOutput.Columns.Add("BS08", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "09"); dtOutput.Columns.Add("BS09", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "10"); dtOutput.Columns.Add("BS10", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "11"); dtOutput.Columns.Add("BS11", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "12"); dtOutput.Columns.Add("BS12", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "13"); dtOutput.Columns.Add("BS13", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "14"); dtOutput.Columns.Add("BS14", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "15"); dtOutput.Columns.Add("BS15", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "16"); dtOutput.Columns.Add("BS16", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "17"); dtOutput.Columns.Add("BS17", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "18"); dtOutput.Columns.Add("BS18", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "19"); dtOutput.Columns.Add("BS19", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "20"); dtOutput.Columns.Add("BS20", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "21"); dtOutput.Columns.Add("BS21", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "22"); dtOutput.Columns.Add("BS22", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "23"); dtOutput.Columns.Add("BS23", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "24"); dtOutput.Columns.Add("BS24", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "25"); dtOutput.Columns.Add("BS25", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "26"); dtOutput.Columns.Add("BS26", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "27"); dtOutput.Columns.Add("BS27", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "28"); dtOutput.Columns.Add("BS28", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "29"); dtOutput.Columns.Add("BS29", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "30"); dtOutput.Columns.Add("BS30", typeof(System.String));
                exAgent.SetCellValue(start, idx++, "31"); dtOutput.Columns.Add("BS31", typeof(System.String));
                #endregion
                string TMONTH = "201412";
                foreach (DataRow row in db.Tables["ICC_HRPDC"].Rows)
                {
                    row["ARTTYPE"] = GetGB2312Value(row["ARTTYPE"].ToString().Trim());
                    TMONTH = row["BS99"].ToString();
                }
                exAgent.SetCurrentSheet("YGRHRPDCXS");
                exAgent.CopySheet("细档");  
                
                dtOutput.Merge(db.Tables["ICC_HRPDC"], false, MissingSchemaAction.Ignore);
                int count = dtOutput.Rows.Count;
                exAgent.RangeCopy(4, 50, 5, 4 + count);
                exAgent.DataTableToExcel(dtOutput, x, 1);
                exAgent.SetCellValue(1, 1, 1);
                exAgent.BatchConvert2Number(4, 7, count + 3, 46);
                exAgent.SetCellValue(1, 1, TMONTH);                 
                var qGW = from GW in db.Tables["ICC_HRPDC"].AsEnumerable()
                          group GW by new
                          {
                              PT = GW["HRITPNR"].ToString(),
                          }
                              into g
                              select new
                              {
                                  KEY = g.Key,
                              };
                foreach (var GW in qGW)
                {
                    HRITPNR = GW.KEY.PT;
                    if (HRITPNR.Trim().Length == 0)
                        continue; 
                    exAgent.SetCurrentSheet("YGRHRPDCXS");
                    exAgent.CopySheet(HRITPNR.Replace("/", "_"));
                    DataRow[] rows = db.Tables["ICC_HRPDC"].Select("HRITPNR='" + HRITPNR + "'");
                    DataTable dtSoruce = rows.CopyToDataTable();
                    dtOutput.Clear();
                    dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);
                    count = dtOutput.Rows.Count;
                    exAgent.RangeCopy(4, 50, 5, 4 + count);
                    exAgent.DataTableToExcel(dtOutput, x, 1);
                    exAgent.SetCellValue(1, 1, 1);
                    exAgent.BatchConvert2Number(4, 7, count + 3, 46);
                    exAgent.SetCellValue(1, 1, TMONTH); 
                }
                exAgent.DeleteSheet("YGRHRPDCXS");
                exAgent.SetCurrentSheet("细档");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("完成", StatusEventType.StopProgress);
            }
        }

    }
}